package com.yinp.proapp.module.im.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.databinding.ItemMessageImageLeftBinding;
import com.yinp.proapp.databinding.ItemMessageImageRightBinding;
import com.yinp.proapp.databinding.ItemMessageTextLeftBinding;
import com.yinp.proapp.databinding.ItemMessageTextRightBinding;
import com.yinp.proapp.module.im.ImConstant;
import com.yinp.proapp.module.im.bean.ImMessageBean;
import com.yinp.proapp.utils.GlideUtils;

import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/11/26
 * package   :com.yinp.proapp.module.im.adapter
 * describe  :
 */
public class ImMessageAdapter extends RecyclerView.Adapter<ImMessageViewHolder> {
    private List<ImMessageBean> mList;
    private Context mContext;

    public ImMessageAdapter(List<ImMessageBean> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {

        return setViewType(position);
    }


    @NonNull
    @Override
    public ImMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull ImMessageViewHolder holder, int position) {
        ImMessageBean bean = mList.get(position);
        if (holder instanceof ImTextViewHolder) {
            ImTextViewHolder viewHolder = (ImTextViewHolder) holder;
            if (bean.isOwn()) {
                viewHolder.bindingTWo.tvContent.setText(bean.getContentText());
                GlideUtils.intoRadius(mContext, bean.getHeadUrl(), viewHolder.bindingTWo.ivHead, 6);
            } else {
                viewHolder.bindingOne.tvContent.setText(bean.getContentText());
                GlideUtils.intoRadius(mContext, bean.getHeadUrl(), viewHolder.bindingOne.ivHead, 6);
            }
        } else if (holder instanceof ImImageHolder) {
            ImImageHolder viewHolder = (ImImageHolder) holder;
            if (bean.isOwn()) {
                GlideUtils.intoRadius(mContext, bean.getHeadUrl(), viewHolder.bindingTWo.ivHead, 6);
                GlideUtils.intoRadius(mContext, bean.getContentText(), viewHolder.bindingTWo.ivContent, 6);
            } else {
                GlideUtils.intoRadius(mContext, bean.getHeadUrl(), viewHolder.bindingOne.ivHead, 6);
                GlideUtils.intoRadius(mContext, bean.getContentText(), viewHolder.bindingOne.ivContent, 6);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private int setViewType(int position) {
        ImMessageBean messageBean = mList.get(position);
        return messageBean.getMessageType();
    }

    private ImMessageViewHolder getViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ImConstant.ImMessage.MessageType.TEXT_OTHER:
                return new ImTextViewHolder(ItemMessageTextLeftBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
            case ImConstant.ImMessage.MessageType.TEXT_OWN:
                return new ImTextViewHolder(ItemMessageTextRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
            case ImConstant.ImMessage.MessageType.URL_IMAGE_OTHER:
                return new ImImageHolder(ItemMessageImageLeftBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
            case ImConstant.ImMessage.MessageType.URL_IMAGE_OWN:
                return new ImImageHolder(ItemMessageImageRightBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
        }
        return null;
    }
}
